//---------------Sonic CD UFO Power Up Script-----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
private alias Object.Value0 : Object.ExplosionDir
private alias Object.Value1 : Object.ExplosionXVel
private alias Object.Value2 : Object.ExplosionYVel
private alias Object.Value3 : Object.Timer
private alias Object.Value4 : Object.YVelocity
private alias Object.Value5 : Object.ScreenDepth // Not used in this script, but set by the parent UFO Object upon spawning
private alias Object.Value6 : Object.ExplosionTimer
private alias Object.Value7 : Object.StartResults

// Player aliases
private alias Object.State : SSSonic.State

private alias 9 : SSSONIC_CAMERAPAN

// Stage Finish Aliases
private alias 8 : STAGEFINISH_LOADTIMEATTACK
private alias 9 : STAGEFINISH_LOADCREDITS

// SFX aliases
private alias 22 : SFX_G_EXPLOSION

// Game Mode Aliases
private alias 2 : MODE_TIMEATTACK


event ObjectDraw
	// If the Object isn't falling at a rate of 4px downwards per frame yet, then update movements
	if Object.YVelocity < 0x40000
		
		// Draw the Power Up icon
		DrawSpriteScreenXY(Object.PropertyValue, Object.iXPos, Object.iYPos)
		
		// Apply an eighth pixel's worth of gravity per frame
		Object.YVelocity += 0x2000
		
		// And then update the Power Up's position itself
		Object.YPos += Object.YVelocity
	end if
	
	// Create explosions as needed
	Object.ExplosionTimer++
	if Object.ExplosionTimer == 6
		Object.ExplosionTimer = 0
		PlaySfx(SfxName[Explosion], 0)
		
		// Randomise the explosion offset a tad bit
		Rand(temp0, 48)
		temp0 -= 24
		temp0 += Screen.XOffset
		temp0 <<= 16
		temp0 += Object.ExplosionXVel
		
		Rand(temp1, 48)
		temp1 -= 24
		temp1 += Screen.YOffset
		temp1 <<= 16
		temp1 += Object.ExplosionYVel
		
		CreateTempObject(TypeName[Smoke Puff], 0, temp0, temp1)
		
		Object[TempObjectPos].DrawOrder = 4
	end if
	
	// This value is randomised by the parent UFO object already, this Object doesn't set it itself
	if Object.ExplosionDir > 50
		// Moving to the bottom right corner of the screen
		
		Object.ExplosionXVel += 0x20000
	else
		// Moving to the bottom left corner of the screen
		
		Object.ExplosionXVel -= 0x20000
	end if
	
	// Make the explosions gravitate to the bottom corner of the screen, at a rate of 0.75 pixels per frame
	Object.ExplosionYVel += 0xC000
	
	Object.Timer++
	if Object.Timer == 160
		if Object.StartResults == true
			// If this object was the spawned by the last UFO, then end the stage
			// -> StartResults is set by the parent UFO when spawning this Power Up
			
			if Options.GameMode < MODE_TIMEATTACK
				if Stage.actnum < 8
					// If in a normal Special Stage, then hand the rest of the ending sequence to Sonic
					
					SSSonic[2].State = SSSONIC_CAMERAPAN
				else
					// If in the hidden Robotnik Special Stage, then go ahead and load the credits
					
					Object[30].Type = TypeName[Stage Finish]
					Object[30].State = STAGEFINISH_LOADCREDITS
					Object[30].DrawOrder = 7
				end if
			else
				// If in time attack, then go ahead and fade out to the TA Menu scene
				
				Object[30].Type = TypeName[Stage Finish]
				Object[30].State = STAGEFINISH_LOADTIMEATTACK
				Object[30].DrawOrder = 0
			end if
		end if
		
		Object.Type = TypeName[Blank Object]
	end if
	
end event


event ObjectStartup
	
	LoadSpriteSheet("3D_Test/Objects.gif")
	
	// 0 - Ring Icon
	SpriteFrame(-8, -8, 16, 16, 1, 67)
	
	// 1 - Speed Shoes Icon
	SpriteFrame(-8, -8, 16, 16, 18, 67)
	
	// 2 - Clock Icon
	SpriteFrame(-8, -8, 16, 16, 35, 67)
	
end event


// ========================
// Editor events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("3D_Test/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 1, 67)

	// Although used by the object, it shouldn't be set by the editor
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
